#include "board.h"
#include "ds18b20.h"
#include "RFM70.h"
#include <stdio.h>

#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)

u8 payload[RFM70_MAX_PACKET_LEN];

void send(s16 temperature) {
	if (txTimeout()) {
		flushTxFIFO();
		cliTimeout();
	}
	sendPayload2((uint8_t *)&temperature, 2);
}

const uint8_t x[][2] = {
 // address data
 { (0x20|0x00), 0x0F }, //Disable CRC ,CRC=1byte, POWER UP, TX
 { (0x20|0x01), 0x3F }, //Enable auto acknowledgement data pipe0-5
 { (0x20|0x02), 0x3F }, //Enable RX Addresses pipe0-5
 { (0x20|0x03), 0x03 }, //RX/TX address field width 5byte
 { (0x20|0x04), 0x08 }, //x = 250 ms = 4000ms, y = 15 tries
 { (0x20|0x05), 0x17 }, //channel = 0x17
 { (0x20|0x06), 0x3F }, //air data rate-2M,out power 5dbm,setup LNA gain high (0dBM)
 { (0x20|0x07), 0x07 }, //
 { (0x20|0x08), 0x00 }, //
 { (0x20|0x09), 0x00 }, //
 { (0x20|0x0C), 0xc3 }, //LSB Addr pipe 2
 { (0x20|0x0D), 0xc4 }, //LSB Addr pipe 3
 { (0x20|0x0E), 0xc5 }, //LSB Addr pipe 4
 { (0x20|0x0F), 0xc6 }, //LSB Addr pipe 5
 { (0x20|0x11), 0x20 }, //Payload len pipe0
 { (0x20|0x12), 0x20 }, //Payload len pipe0
 { (0x20|0x13), 0x20 }, //Payload len pipe0
 { (0x20|0x14), 0x20 }, //Payload len pipe0
 { (0x20|0x15), 0x20 }, //Payload len pipe0
 { (0x20|0x16), 0x20 }, //Payload len pipe0
 { (0x20|0x17), 0x20 }, //Payload len pipe0
 { (0x20|0x1C), 0x3F }, //Enable dynamic payload legth data pipe0-5
 { (0x20|0x1D), 0x07 } //Enables Dynamic Payload Length,Enables Payload with ACK
};

int main(void) {
	init();
	DBG(("\n\rinit 5\n\r"));
	DS18B20_init();
	DS18B20_Convert();

	setPower(0);
	Begin(-1);
	setModeTX();
	s16 read = DS18B20_Read();

	DBG(("\n\r%d",read));
	send(read);
	//delayMs(5000);
	setPower(0);
	IWDG->KR = 0x5555;
	IWDG->PR = 7;
	IWDG->RLR = 0xFFF;
	IWDG->KR = 0xAAAA;
	IWDG->KR = 0xCCCC;
	deinit();
	PWR_EnterSTANDBYMode();
	return 0;
}
